//
//  ViewController1.m
//  连连看初
//
//  Created by administrator on 16/6/2.
//  Copyright © 2016年 mark. All rights reserved.
//

#import "ViewController1.h"
#import "ViewController.h"
#import "AppDelegate.h"
#import  <AVFoundation/AVFoundation.h>
@interface ViewController1 ()<AVAudioPlayerDelegate>{
    AVAudioPlayer *player;
}
@property __block int num;
@end
@implementation ViewController1
-(void)viewWillAppear:(BOOL)animated{
    //if (self.backgroung.isAnimating) return;
    //1.建立数组，并且把图片加载到数组中
    NSMutableArray *images = [NSMutableArray array];//建立数组时一定要初始化。
    for (int i = 0; i<8; i++) {
        //找到所有的图片
        NSString *filename = [NSString stringWithFormat:@"%d.png",i];
        NSString *path = [[NSBundle mainBundle] pathForResource:filename ofType:nil];
        UIImage *image = [UIImage imageWithContentsOfFile:path];
        [images addObject:image];
    }
    //2.设置动画
    self.backgroung.animationImages = images;
    self.backgroung.animationDuration =images.count*0.15;
    self.backgroung.animationRepeatCount = 200;
    //3.播放动画
    [self.backgroung startAnimating];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.num=4;
    NSString *path = [[NSBundle mainBundle] pathForResource:@"3530" ofType:@"mp3"];
    //2.将路径转为NSURL
    NSURL *url = [[NSURL alloc] initFileURLWithPath:path];
    //3.创建音乐对象
    player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
    //4.设置音乐对象的属性
    player.delegate = self;
    player.numberOfLoops = 20;//播放次数
    player.volume = 2; //音量范围:0.5~1.0
    //5.播放
    [player prepareToPlay];
    [player play];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)rank:(id)sender {
    UIAlertController*alert=[UIAlertController alertControllerWithTitle:@"难度选择" message:nil preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *d1=[UIAlertAction actionWithTitle:@"简单" style:UIAlertActionStyleDefault handler:^(UIAlertAction *_Nonnull action){
        self.num = 4;
    }];
    UIAlertAction *d2=[UIAlertAction actionWithTitle:@"一般" style:UIAlertActionStyleDefault handler:^(UIAlertAction *_Nonnull action){
        self.num = 6;
    }];
    UIAlertAction *d3=[UIAlertAction actionWithTitle:@"困难" style:UIAlertActionStyleDefault handler:^(UIAlertAction *_Nonnull action){ self.num = 8;}];
    [alert addAction:d1];[alert addAction:d2];[alert addAction:d3];
    [self presentViewController:alert animated:YES completion:nil];
}
- (IBAction)about:(id)sender {
    UIAlertController *alert=[UIAlertController alertControllerWithTitle:@"关于" message:@"游戏说明：1.玩家在水果隐藏的情况下，找出相同的水果并把它消除；在规定的时间内玩家需要把所有的水果全部消除才能通关。2.关卡难度分为：简单、一般和困难三个难度。                                                制作团队：段正龙、陈浩、贺智峰。" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *ok=[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    }];
    [alert addAction:ok];
    [self presentViewController:alert animated:YES completion:nil];
}
- (IBAction)hightscore:(id)sender {
    AppDelegate *del = [[UIApplication sharedApplication]delegate];
    double score1 = del.heightestScore1;
    double score2 = del.heightestScore2;
    double score3 = del.heightestScore3;
    NSString *str1 = [[NSString alloc] initWithFormat:@"简单模式：%.2f s",score1];
    NSString *str2 = [[NSString alloc] initWithFormat:@"一般模式：%.2f s",score2];
    NSString *str3 = [[NSString alloc] initWithFormat:@"困难模式：%.2f s",score3];
    UIActionSheet *actionSheet = [[UIActionSheet alloc]
                                  initWithTitle:@"最佳成绩"
                                  delegate:nil
                                  cancelButtonTitle:@"OK"
                                  destructiveButtonTitle:nil
                                  otherButtonTitles:str1,str2,str3,nil];
    actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
    [actionSheet showInView:self.view];
   
}
- (IBAction)playgame:(id)sender {
    ViewController*vc=(ViewController*)[self.storyboard instantiateViewControllerWithIdentifier:@"game"];
    
    vc.rak =[NSString stringWithFormat:@"%d",self.num];
    [self presentViewController:vc animated:YES completion:nil];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"ready go" ofType:@"mp3"];
    //2.将路径转为NSURL
    NSURL *url = [[NSURL alloc] initFileURLWithPath:path];
    //3.创建音乐对象
    player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
    //4.设置音乐对象的属性
    player.delegate = self;
    player.numberOfLoops = 0;//播放次数
    player.volume = 5;
    //5.播放
    [player prepareToPlay];
    [player play];
    //4.清除内存
     CGFloat delay = self.backgroung.animationDuration+1.0;
    [self.backgroung performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:delay];
}
@end
